//
//  RecentSystemCell.m
//  SportSocial
//
//  Created by yull on 15/1/28.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "RecentSystemCell.h"
#import "FriendNet.h"
#import "GroupNet.h"
#import "ChatInfoDao.h"
#import <UIImageView+WebCache.h>
#import "NSString+Size.h"

@implementation RecentSystemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGRect rect = CGRectMake(self.contentView.width - 10 - 56, self.nameLabel.bottom + 5, 56, 25);
        self.agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.agreeButton.frame = rect;
        self.agreeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        self.agreeButton.backgroundColor = RGB(222, 65, 16);
        self.agreeButton.layer.cornerRadius = self.agreeButton.height / 2;
        [self.agreeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.agreeButton setTitle:@"同意" forState:UIControlStateNormal];
        self.agreeButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [self.contentView addSubview:self.agreeButton];
        
        rect.origin.x -= (rect.size.width + 5);
        self.refuseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.refuseButton.frame = rect;
        self.refuseButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        self.refuseButton.backgroundColor = RGB(123, 171, 176);
        self.refuseButton.layer.cornerRadius = self.agreeButton.height / 2;
        [self.refuseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.refuseButton setTitle:@"拒绝" forState:UIControlStateNormal];
        self.refuseButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [self.contentView addSubview:self.refuseButton];
        
        self.contentLabel.numberOfLines = 0;
//        [self.agreeButton addTarget:self action:@selector(onClickAgreeButton:) forControlEvents:UIControlEventTouchUpInside];
//        [self.refuseButton addTarget:self action:@selector(onClickRefuseButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

+ (CGFloat)calculateCellHeightWithText:(ChatSystemInfo *)info
{
    CGSize constranedSize = CGSizeMake(SCREEN_WIDTH -70 - 10, CGFLOAT_MAX);
    CGSize labelSize = [info.content getSizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedSize:constranedSize];
    
    CGFloat actionButtonHeight = 0;
    if (info.infoType == SystemInfoTypeFriend) {
        if (info.friendType == SystemInfoFriendTypeApply) {
            actionButtonHeight = 30;
        }
    }else {
        if (info.groupType == SystemInfoGroupTypeInvite || info.groupType == SystemInfoGroupTypeApply) {
            actionButtonHeight = 30;
        }
    }
    
    return labelSize.height + 50 + actionButtonHeight;
}

- (void)drawCellWithInfo:(ChatSystemInfo *)info
{
    self.info = info;
    
//    self.headImageView.backgroundColor = [UIColor cyanColor];
    
    UIImage *placeholderImage = info.infoType == SystemInfoTypeFriend ? [UIImage imageNamed:@"zc_photo_bg"] : [UIImage imageNamed:@"qun"];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:info.icon] placeholderImage:placeholderImage];
    
    if (info.infoType == SystemInfoTypeFriend) {
        self.nameLabel.text = info.userName;
    }else {
        self.nameLabel.text = info.groupName;
    }
    
    self.contentLabel.text = info.content;
    
    CGSize constranedSize = CGSizeMake(SCREEN_WIDTH -70 - 10, CGFLOAT_MAX);
    CGSize labelSize = [info.content getSizeWithFont:self.contentLabel.font constrainedSize:constranedSize];
    self.contentLabel.height = labelSize.height;
    
    self.agreeButton.top = self.contentLabel.bottom + 5;
    self.refuseButton.top = self.contentLabel.bottom + 5;
    
    self.timeLabel.text = [FunctionMethod getChatTimeTextWithTimeStamp:info.time];
    
    CGFloat nameWidth = [self.nameLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, self.nameLabel.height)].width;
    self.nameLabel.width = nameWidth;
    
    if (info.infoType == SystemInfoTypeFriend) {
        if (info.friendType == SystemInfoFriendTypeApply) {
            self.agreeButton.hidden = NO;
            self.refuseButton.hidden = NO;
        }else {
            self.agreeButton.hidden = YES;
            self.refuseButton.hidden = YES;
        }
    }else {
        if (info.groupType == SystemInfoGroupTypeInvite || info.groupType == SystemInfoGroupTypeApply) {
            self.agreeButton.hidden = NO;
            self.refuseButton.hidden = NO;
        }else {
            self.agreeButton.hidden = YES;
            self.refuseButton.hidden = YES;
        }
    }
}

- (void)onTapAvatar:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(onClickRecentAvatarView:)]) {
        [self.delegate onClickRecentAvatarView:self.info];
    }
}

@end
